package com.tubeonfire.controller;

import java.io.IOException;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.tubeonfire.entity.SiteConfig;
import com.tubeonfire.model.admin.SiteConfigModel;

@SuppressWarnings("serial")
public class ReportErrorServlet extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			SiteConfig siteConfig = SiteConfigModel.get();
			String id = req.getParameter("id");
			StringBuilder mailContent = new StringBuilder();
			mailContent.append("User has reported video error <br/>");
			mailContent.append("Please visit <a href=\"http://"
					+ req.getServerName() + "/video/" + id
					+ "/error_page.html\">this link</a> to check again !");
			Properties props = new Properties();
			Session sessions = Session.getDefaultInstance(props, null);

			String msgBody = mailContent.toString();
			try {
				Message msg = new MimeMessage(sessions);
				msg.setSubject("Tubeonfire - Video Error Report");
				msg.setFrom(new InternetAddress(siteConfig.getAdminEmail(),
						"Tubeonfire Assistant"));
				msg.addRecipient(Message.RecipientType.TO, new InternetAddress(
						siteConfig.getAdminEmail(), siteConfig.getAdminEmail()));
				msg.setContent(msgBody, "text/html");
				Transport.send(msg);

			} catch (AddressException e) {
				e.printStackTrace();
				resp.getWriter().println("Error when sending active email.");
			} catch (MessagingException e) {
				e.printStackTrace();
				resp.getWriter().println("Error when sending active email.");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
